/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import DTO.TinTucDTO;
import DTO.XpathDTO;
import java.util.ArrayList;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author QuangHau
 */
public class Test extends javax.swing.JDialog {    
    
    private ArrayList<TinTucDTO> list;
    private ArrayList<TinTucDTO> list2;
    int status;
   
    public Test(java.awt.Frame parent, boolean modal, Object _list, int _status) {
        super(parent, modal);
        initComponents();
        
        status = _status;
        if(status ==1){
           list = (ArrayList<TinTucDTO>)_list;
           this.setTitle("Tin tức mới");
           btnTrang.setVisible(false);
           Main.reset = true;
           pnTinTuc.setBorder(javax.swing.BorderFactory.createTitledBorder(
                   javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Danh sách tin tức mới"));
        }
        else{
            Object[] arr = (Object[])_list;
            list = (ArrayList<TinTucDTO>)arr[0];
            list2 = (ArrayList<TinTucDTO>)arr[1];
        }
      
    }
    
    private void loadTin(int option){
        JPanel pn = new JPanel();
        pn.setLayout(new BoxLayout(pn, BoxLayout.Y_AXIS));
        if (option == 1) {
            if (list != null && list.size() >0) {
                for (int i = 0; i < list.size(); i++) {
                    pn.add(new pnNews(list.get(i), status));
                }
                pnTinTuc.getViewport().add(pn);
            } else {
                pnTinTuc.getViewport().add(new JLabel("Không có tin tức"));
            }
        }
        else{
            if (list2 != null && list2.size() >0) {
                for (int i = 0; i < list2.size(); i++) {
                    pn.add(new pnNews(list2.get(i), status));
                }
                pnTinTuc.getViewport().add(pn);
            } else {
                pnTinTuc.getViewport().add(new JLabel("Không có tin tức"));
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnTinTuc = new javax.swing.JScrollPane();
        btnThoat = new javax.swing.JButton();
        btnTrang = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Kiểm tra");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        pnTinTuc.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Danh sách tin tức trang thứ nhất"));

        btnThoat.setText("Thoát");
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        btnTrang.setText("Trang 2");
        btnTrang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTrangActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnTinTuc, javax.swing.GroupLayout.DEFAULT_SIZE, 605, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnTrang)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnThoat)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnThoat, btnTrang});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnTinTuc, javax.swing.GroupLayout.DEFAULT_SIZE, 420, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnThoat)
                    .addComponent(btnTrang))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        loadTin(1);
    }//GEN-LAST:event_formWindowOpened

    private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnThoatActionPerformed

    private void btnTrangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTrangActionPerformed
        if(btnTrang.getText().equals("Trang 2")){
             pnTinTuc.setBorder(javax.swing.BorderFactory.createTitledBorder(
                   javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Danh sách tin tức trang thứ hai"));
            loadTin(2);
            btnTrang.setText("Trang 1");
        }
        else{
            pnTinTuc.setBorder(javax.swing.BorderFactory.createTitledBorder(
                   javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Danh sách tin tức trang thứ nhất"));
            loadTin(1);
            btnTrang.setText("Trang 2");
        }
    }//GEN-LAST:event_btnTrangActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Test dialog = new Test(new javax.swing.JFrame(), true,null, 1);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnThoat;
    private javax.swing.JButton btnTrang;
    private javax.swing.JScrollPane pnTinTuc;
    // End of variables declaration//GEN-END:variables
}
